import { Body, Controller, Get, Post, Query, UseGuards } from '@nestjs/common'
import { ArticleService } from './article.service'
import { ArticleCreateDTO } from './dto/article-create.dto'
import { ArticleEditDTO } from './dto/article-edit.dto'
import { IdDTO } from './dto/id.dto'
import { ListDTO } from './dto/list.dto'
import { ApiTags, ApiOkResponse, ApiBearerAuth } from '@nestjs/swagger'
import { ArticleListVO, ArticleListResponse } from './vo/article-list.vo'
import { ArticleInfoVO, ArticleInfoResponse } from './vo/article-info.vo'
import { AuthGuard } from '@nestjs/passport'

@ApiTags('文章模块')
@Controller('article')
export class ArticleController {
  constructor(private articleService: ArticleService) {}

  @Get('list')
  @ApiOkResponse({ description: '文章列表', type: ArticleListResponse })
  async getList(@Query() listDTO: ListDTO): Promise<ArticleListVO> {
    return await this.articleService.getList(listDTO)
  }

  @Get('info')
  @ApiOkResponse({ description: '文章详情', type: ArticleInfoResponse })
  async getInfo(@Query() idDTO: IdDTO): Promise<ArticleInfoVO> {
    return this.articleService.getInfo(idDTO)
  }

  @UseGuards(AuthGuard('jwt'))
  @Post('create')
  @ApiBearerAuth()
  @ApiOkResponse({ description: '创建文章', type: ArticleInfoResponse })
  async create(
    @Body() articleCreateDTO: ArticleCreateDTO,
  ): Promise<ArticleInfoVO> {
    return await this.articleService.created(articleCreateDTO)
  }

  @UseGuards(AuthGuard('jwt'))
  @Post('edit')
  @ApiBearerAuth()
  @ApiOkResponse({ description: '编辑文章', type: ArticleInfoResponse })
  async update(@Body() articleEditDTO: ArticleEditDTO): Promise<ArticleInfoVO> {
    return await this.articleService.update(articleEditDTO)
  }

  @UseGuards(AuthGuard('jwt'))
  @Post('delete')
  @ApiBearerAuth()
  @ApiOkResponse({ description: '删除文章', type: ArticleInfoResponse })
  async delete(@Body() idDTO: IdDTO): Promise<ArticleInfoVO> {
    return await this.articleService.delete(idDTO)
  }
}
